package com.incar.chuxing.utils.Dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.incar.chuxing.R;
import com.incar.chuxing.bean.CheckUserAccountBean;
import com.incar.chuxing.utils.SPKey;
import com.incar.chuxing.utils.SPUtils;

import java.util.List;

public class NeedRechargeDialog extends Dialog {
    public static final int SEND_CAR_DAILY_RENT = 1;
    public static final int NORMAL_DAILY_RENT = 0;
    Context mContext;
    private RecyclerView confirmRc;
    private TextView choose_cancel;
    private TextView choose_ok;
    private List<CheckUserAccountBean.AttachmentBean> confirmData;
    private OnNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private OnYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
    private int showType;

    public NeedRechargeDialog(Context context) {
        super(context, R.style.navi_dialog);
        this.mContext = context;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_confirm_recharge);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        window.setWindowAnimations(R.style.dialog_choose_navi);
        initData();
        initView();
        initEvent();
    }


    public void setData(List<CheckUserAccountBean.AttachmentBean> confirmData) {
        this.confirmData = confirmData;
    }


    /**
     * 初始化界面的确定和取消监听器
     */
    private void initData() {

    }


    /**
     * 初始化界面控件
     */
    private void initView() {
        choose_cancel = (TextView) findViewById(R.id.confirm_cancel);
        choose_ok = (TextView) findViewById(R.id.confirm_ok);
        confirmRc = findViewById(R.id.confirm_rc);
        confirmRc.setLayoutManager(new LinearLayoutManager(mContext));
        ConfirmPriceAdapter confirmPriceAdapter = new ConfirmPriceAdapter(confirmData);
        confirmRc.setAdapter(confirmPriceAdapter);

        String tips1 = SPUtils.getInstance(mContext).getStringValue(SPKey.FIND_RECHARGE_TIP, "日租服务需要您预付金额到账户余额，充值金额可用于最终订单支付。");
        ((TextView) findViewById(R.id.confirm_tips)).setText(tips1);
    }


    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        choose_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        choose_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }


    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(OnNoOnclickListener onNoOnclickListener) {

        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(OnYesOnclickListener onYesOnclickListener) {
        this.yesOnclickListener = onYesOnclickListener;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface OnYesOnclickListener {
        void onYesClick();
    }

    public interface OnNoOnclickListener {
        void onNoClick();
    }


    class ConfirmPriceAdapter extends BaseQuickAdapter<CheckUserAccountBean.AttachmentBean, BaseViewHolder> {
        public ConfirmPriceAdapter(List<CheckUserAccountBean.AttachmentBean> data) {
            super(R.layout.item_confirm_fee, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, CheckUserAccountBean.AttachmentBean item) {
            helper.setText(R.id.item_title, item.getTitle());
            helper.setText(R.id.item_content, item.getContent());
            if (helper.getAdapterPosition() == confirmData.size() - 1) {
                ((TextView) helper.getView(R.id.item_content)).setTextColor(0xffff4b41);
            }
        }
    }
}